package org.sport.presenter;

import net.sf.json.JSONObject;
import org.sport.logic.LogicController;
import org.sport.logic.Logic_Sport;
import org.sport.model.jsonModel.JsonUser;
import org.sport.model.jsonModel.JsonUserImg;
import org.sport.model.jsonModel.Json_0_set;
import org.sport.model.jsonModel.Json_2_state_plus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 * Created by Allen on 2017/9/11.
 *
 * 用以对接前端，解析前端数据，发送数据给前端，不加逻辑控制的原因是因为可能出现协议解析，否则耦合性过高
 * 也方便后面扩展
 */



@Controller
public class AndroidInterface {


    @Autowired
    LogicController logicController;

    @Autowired
    Logic_Sport logicForsport;

    @RequestMapping(value = "/0/upload_img", method = RequestMethod.POST)
    public void upload(@RequestParam(value = "icon" , required = false)MultipartFile file
                        ,@RequestParam("phoneNumber") String phone
                        ,@RequestParam("nickname") String name
                        ,ModelMap model
                        , HttpServletRequest request
                        , HttpServletResponse response) {

        System.out.println("图片上传开始");

        JsonUserImg jsonUserImg = new JsonUserImg();
        jsonUserImg.setPhoneNumber(phone);
        jsonUserImg.setNickname(name);

        jsonUserImg = logicController.upLoadImg(jsonUserImg,file,request);

      //  model.addAttribute("fileURL",request.getContextPath()+"/0/upload_img"+fileName);
        //return "../testpage/result";
        try {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().print(JSONObject.fromObject(jsonUserImg).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/0/set_old", method = RequestMethod.POST)
    public void setRequest(
            @RequestBody Json_0_set jsonUser
            , HttpServletRequest request
            , HttpServletResponse response) {
        Json_0_set jsonUserImg = new Json_0_set();
        jsonUserImg.setPhoneNumber("123456");
        jsonUserImg = logicController.requestUserImg(jsonUser);

        //  model.addAttribute("fileURL",request.getContextPath()+"/0/upload_img"+fileName);
        //return "../testpage/result";
        try {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().print(JSONObject.fromObject(jsonUserImg).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*
    * 登录的接口，0代表登录界面，1代表首页等界面，2代表联系人？商榷中
    * 数据传输方式1：
    *               server      ->      android  :   json
    *               android     ->      server   :   url
    *
    * */
    @RequestMapping(value = "/0/sign_in_t2", method = RequestMethod.POST)
    public void userSignInT2(@RequestParam("phoneNumber") String phone
            , @RequestParam("password") String password
            , HttpServletRequest request
            , HttpServletResponse response
    ) {

        System.out.println("sign_in requestParams 方式2");
        JsonUser jsonUser = new JsonUser();
        jsonUser.setPhoneNumber(phone);
        jsonUser.setPassword(password);
        jsonUser = logicController.SignIn(jsonUser);
        System.out.println(jsonUser.toString());
        try {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().print(JSONObject.fromObject(jsonUser).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*
    * 注册的接口，0代表登录界面，1代表首页等界面，2代表联系人？商榷中
    * 数据传输方式1：
    *               server      ->      android  :   json
    *               android     ->      server   :   url
    *
    * */
    @RequestMapping(value = "/0/sign_up_t2", method = RequestMethod.POST)
    public void userSignUp(@RequestParam("phoneNumber") String phone
            , @RequestParam("password") String password
            , HttpServletRequest request
            , HttpServletResponse response
    ) {
        System.out.println("sign_up requestParams 方式2");
        JsonUser jsonUserup = new JsonUser();
        jsonUserup.setPhoneNumber(phone);
        jsonUserup.setPassword(password);
        jsonUserup = logicController.SignUp(jsonUserup);
        try {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().print(JSONObject.fromObject(jsonUserup).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /*
    * 注册的接口，0代表登录界面，1代表首页等界面，2代表联系人？商榷中
    * 数据传输方式1：
    *               server      ->      android  :   json
    *               android     ->      server   :   url
    *
    * 数据传输方式2：
    *               server      ->      android  :   json
    *               android     ->      server   :   json
    *
    * */
    @RequestMapping(value = "/0/sign_up_old", method = RequestMethod.POST)
    public void userSignUp(@RequestBody JsonUser jsonUserup
            , HttpServletRequest request
            , HttpServletResponse response
    ) {

        jsonUserup = logicController.SignUp(jsonUserup);
        try {
            response.getWriter().print(JSONObject.fromObject(jsonUserup).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /*
   * 测试数据传输方式2 json 2 json
   * 数据传输方式2：
   *               server      ->      android  :   json
   *               android     ->      server   :   json
   *
   * */
    @RequestMapping(value = "/0/sign_intest", method = RequestMethod.POST)
    public void userTest(@RequestBody JsonUser jsonUser
            , HttpServletRequest request
            , HttpServletResponse response) {


        jsonUser = logicController.SignIn(jsonUser);

        System.out.println(jsonUser.toString());
        System.out.println(jsonUser.getPassword());
        System.out.println(jsonUser.getPhoneNumber());
        System.out.println(jsonUser.getResult());

/*        modelAndView.addObject("time", new Date());
        modelAndView.setViewName("../../resource/testpage/test.jsp?"+ JSONObject.fromObject(userTest).toString());*/

        try {
            response.getWriter().print(JSONObject.fromObject(jsonUser).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 功能：记录运动以及运动轨迹
     * json参数：包含运动的一系列情况
     * 返回：添加了增加记录的结果
     */
    @RequestMapping(value = "/2/state_plus", method = RequestMethod.POST,consumes="application/json",produces="application/json")
    public void addSportPlus(@RequestBody Json_2_state_plus json_2_state_plus
            , HttpServletRequest request
            , HttpServletResponse response
    )
    {
        System.out.println("plus");
        json_2_state_plus = logicForsport.saveSportPlus(json_2_state_plus);
        try {
            response.getWriter().print(JSONObject.fromObject(json_2_state_plus).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}